package org.alvin.mp3.download;

import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;


/**
 * 爱听音乐
 */
public class Nzb1Download implements IDownload {
    @Override
    public void download(String search, String downloadDir, JTextArea textArea) throws IOException, InterruptedException {
        textArea.setText("");
        textArea.append("弄歌宝开始下载");
        textArea.append("\n");
        String[] lines = search.trim().split("\n");
        for (String line : lines) {
            downloadMp3(line.trim(), downloadDir, textArea);
            textArea.append("等待===防止IP被禁止");
            textArea.append("\n");
            TimeUnit.SECONDS.sleep((long) (Math.random() * 10));
            textArea.append("等待完成");
            textArea.append("\n");
        }
    }

    private void downloadMp3(String name, String downloadDir, JTextArea textArea) throws IOException, InterruptedException {
        String url = "https://www.1nzb.com/search?ac=";
        url = url + URLEncoder.encode(name, "utf-8");
        File file = new File(downloadDir);
        //
        Document document = Jsoup.connect(url).get();
        Element element = document.getElementsByClass("mul").get(0);
        Elements rootElements = element.getElementsByTag("a");
        for (int i = 0; i < rootElements.size(); i++) {
            textArea.append("等待===防止IP被禁止");
            textArea.append("\n");
            TimeUnit.SECONDS.sleep((long) (Math.random() * 10));
            textArea.append("等待完成");
            textArea.append("\n");
            System.out.println(rootElements.get(i).attr("href") + "====" + rootElements.get(i).text());
            url = "https://www.1nzb.com/" + rootElements.get(i).attr("href");
            String href = rootElements.get(i).attr("href");
            //
            document = Jsoup.connect(url).get();
            Elements elements = document.getElementsByAttributeValue("name", "vercode");
            String mp3Url = elements.get(0).attr("value");
            String lrc = document.getElementsByAttributeValue("placeholder", "歌词").text();
            //
            String id = href;
            id = id.substring(id.lastIndexOf("/") + 1, id.lastIndexOf("."));
            id = id.replaceAll("[\\\\/:*?\"<>|]", "");

            String title = document.getElementsByClass("layui-elem-field").get(0).text();
            String[] array = title.split("-");
            title = array[0].trim();
            String author = array[1].trim();
            author = author.substring(0, author.lastIndexOf("."));


            title = title.replaceAll("[\\\\/:*?\"<>|]", "");
            author = author.replaceAll("[\\\\/:*?\"<>|]", "");
            //下载mp3
            HttpUtil.downloadFile(mp3Url, new File(downloadDir, id + "==" + title + "-" + author + ".mp3"));
            String console = "下载mp3:" + title + "-" + author + ".mp3";
            textArea.append(console);
            textArea.append("\n");
            System.out.println(console);
            //下载歌词
            Files.write(Paths.get(file.getAbsolutePath(), id + "==" + title + "-" + author + ".lrc"), lrc.replaceAll("\\[", "\n[").getBytes());
            console = "下载歌词：" + title + "-" + author + ".lrc";
            textArea.append(console);
            textArea.append("\n");
            System.out.println(console);
        }
    }


}
